//
//  UIWebViewController.m
//  WZBlogMagazine
//
//  Created by Sunny on 11-11-22.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "UIWebViewController.h"

#import "Blog.h"

#import "WZ66WZZBAppDelegate.h"
#import "ContentViewController.h"

@implementation UIWebViewController

@synthesize webView;
@synthesize activityIndicator;
@synthesize pageNumber;
@synthesize clickToPageNumber;

/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

// load the view nib and initialize the pageNumber ivar
- (id)initWithPageNumber:(int)page
{
    if (self = [super initWithNibName:@"UIWebViewController" bundle:nil])
    {
        pageNumber = page;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

- (void)dataLoad:(Blog *) blog {
	//
	[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:blog.detailUrl]]];
	webView.scalesPageToFit = YES;
}

- (void )webViewDidStartLoad:(UIWebView *)webView {
	[ activityIndicator startAnimating];
}

- (void )webViewDidFinishLoad:(UIWebView *)webView {
	[activityIndicator stopAnimating];
}



- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *requestString = [[request URL] absoluteString];
    NSArray *components = [requestString componentsSeparatedByString:@":"];
    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"myweb"]) {
        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"]) 
        {
			if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"start"]) {
				[self performSelector:@selector(touch) withObject:nil afterDelay:0.1];
			}
			
			if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"move"]) {
				[UIWebViewController cancelPreviousPerformRequestsWithTarget:self selector:@selector(touch) object:nil];
			}
			
			
        }
        
        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"click"]) 
        {
            self.clickToPageNumber = [(NSString *)[components objectAtIndex:2] intValue];
            [self performSelector:@selector(click) withObject:nil afterDelay:0.1];
        }
        
        return NO;
    }
    return YES;
}


- (void)touch {
	ContentViewController * contentViewController = [WZ66WZZBAppDelegate sharedAppDelegate].contentViewController;
	if ([contentViewController isBarShow]) {
		[contentViewController hiddenBar:YES];
		
	} else {
		[contentViewController showBar:YES];
	}
} 


- (void)click {
	ContentViewController * contentViewController = [WZ66WZZBAppDelegate sharedAppDelegate].contentViewController;
	[contentViewController viewDetail:self.clickToPageNumber];
} 




@end
